package framework.sepal4java.common.struct;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class InvServiceConfig implements ServiceConfig {
    boolean grpOn;
    int invGenThro;
    List<String> chkFiles;

    public InvServiceConfig() {
    }

    public void setGrpOn(boolean grpOn) {
        this.grpOn = grpOn;
    }

    public void setInvGenThro(int invGenThro) {
        this.invGenThro = invGenThro;
    }

    public boolean getGrpOn() {
        return grpOn;
    }

    public int getInvGenThro() {
        return invGenThro;
    }

    public void setChkFiles(List<String> chkFiles) {
        this.chkFiles = chkFiles;
    }

    public List<String> getChkFiles() {
        return chkFiles;
    }

    @Override
    public String toString() {
        return toJSONObject().toJSONString();
    }

    public static InvServiceConfig fromJSONString(String json) {
        InvServiceConfig config = new InvServiceConfig();
        JSONObject jo = JSON.parseObject(json);
        config.grpOn = jo.getBooleanValue("grp_on");
        config.invGenThro = jo.getIntValue("inv_gen_thro");
        if (jo.containsKey("chk_files")) {
            JSONArray ja = jo.getJSONArray("chk_files");
            config.chkFiles = ja.toJavaList(String.class);
        }
        return config;
    }

    @Override
    public String toJSONString() {
        return toString();
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jo = new JSONObject();
        jo.put("grp_on", grpOn);
        jo.put("inv_gen_thro", invGenThro);
        if (chkFiles != null) {
            JSONArray ja = new JSONArray();
            ja.addAll(chkFiles);
            jo.put("chk_files", ja);
        }
        return jo;
    }
}
